using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
/// <summary>
/// Summary description for Logger
/// </summary>
public class Logger
{
    static object _sync = new object();

    public void WriteLog(string Errfilename,string strMsg, string strInTime)
    {
        string strLine = "";
        string strFileName = "";

        string strLogPath = ConfigurationManager.AppSettings["Logpath"];

        strLogPath = strLogPath + @"\" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString();

        DirectoryInfo drInfo = new DirectoryInfo(strLogPath);

        if (!drInfo.Exists)
        {
            drInfo.Create();
        }

        strLine = strInTime + "|" + Errfilename + "|" + strMsg;
        strLine = strLine.Trim();
        strFileName = strLogPath + @"\" + DateTime.Now.ToString("dd-MM-yyyy") + "_IMI_Log.txt";

        StreamWriter fw;
        try
        {
            lock (_sync)
            {
                fw = new StreamWriter(strFileName, true);
                fw.WriteLine(strLine);
                fw.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------");
                fw.Close();
            }
        }
        catch (Exception ex)
        {
            WriteErr(Errfilename, ex.Message.ToString(), DateTime.Now.ToString());
        }
    }

    public void WriteErr(string file,string errMsg, string strInTime)
    {
        string Line = "";
        string filename = "";
        string writerLogPath = ConfigurationManager.AppSettings["Errorpath"].ToString();
        writerLogPath = writerLogPath + @"\" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString();
        DirectoryInfo drInfo = new DirectoryInfo(writerLogPath);

        if (!drInfo.Exists)
        {
            drInfo.Create();
        }

        Line = strInTime + "|"+ file +"|"+ errMsg;
        Line = Line.Trim();

        filename = writerLogPath + @"\" + DateTime.Now.ToString("dd-MM-yyyy") + "_IMI_Error.txt";

        StreamWriter fw;

        try
        {
            lock (_sync)
            {
                fw = new StreamWriter(filename, true);
                fw.WriteLine(Line);
                fw.WriteLine("---------------------------------------------------------------------------");
                fw.Close();
            }
        }
        catch (Exception ex)
        {
            string msg=ex.Message;
        }
    }
    public void WriteClassErr(string errMsg, string strInTime,string funcName,string className)
    {
        string Line = "";
        string filename = "";
        string writerLogPath = ConfigurationManager.AppSettings["ClassErrorpath"].ToString();
        writerLogPath = writerLogPath + @"\" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString();
        DirectoryInfo drInfo = new DirectoryInfo(writerLogPath);

        if (!drInfo.Exists)
        {
            drInfo.Create();
        }

        Line = strInTime + "| " +errMsg+"| "+ funcName + "| "+ className;
        Line = Line.Trim();

        filename = writerLogPath + @"\" + DateTime.Now.ToString("dd-MM-yyyy") + "_IMI_Error.txt";

        StreamWriter fw;

        try
        {
            lock (_sync)
            {
                fw = new StreamWriter(filename, true);
                fw.WriteLine(Line);
                fw.WriteLine("---------------------------------------------------------------------------");
                fw.Close();
            }
        }
        catch (Exception ex)
        {
            string msg = ex.Message;
        }
    }
}
